import pygame
from accident import *
from reactorblock import *
from globalstates import *
from actions import *
from soundmachine import *
        
class GameState(object):    
    
    def AssembleStatusReport(self):
        return GlobalStates.AssembleStatusReport()

    def GetReactors(self):
        return GlobalStates.Reactors
    
    def GetActions(self):
        return self.actions
        
    def AddAccident(accident):
        AccidentList.Add(accident)

    def __init__(self):
        GlobalStates.ResetGlobalStates()        
        self.round = 1
        self.num_workers_left = 9        

        self.actions = (
            Action0(),
            Action1(),
            Action2(),
            Action3(),
            Action4(),
            Action5(),
            Action6(),
            Action7(),            
            Action8()
        )
        
        GlobalStates.AllCounterMeasuresCompleteList = self.actions
                    
        self.accidents = ( 
            Accident0(),
            Accident1(),
            Accident2(),
            Accident3(),
            Accident4(),
            Accident5() )
                
    def tick(self):
        DebugLogger.DebugOut("Round: " + str(self.round) + " ================================")
        GlobalStates.PerformCounterMeasureActions()
        
        DebugLogger.DebugOut("- automated:")
        
        myReport = GlobalStates.AssembleStatusReport()
        for line in myReport:
            print line
        
        GlobalStates.IncreaseCoreTemperature(49)    
                        
        if GlobalStates.Core_Temperature >= 100 and GlobalStates.Core_Temperature < 150:
            GlobalStates.AddAccident(self.accidents[0])
        elif GlobalStates.Core_Temperature >= 150 and GlobalStates.Core_Temperature < 200:
            GlobalStates.AddAccident(self.accidents[1])    
        elif GlobalStates.Core_Temperature >= 200 and GlobalStates.Core_Temperature < 250:
            GlobalStates.AddAccident(self.accidents[2])           
        elif GlobalStates.Core_Temperature >= 250 and GlobalStates.Core_Temperature < 300:
            GlobalStates.AddAccident(self.accidents[3])          
        elif GlobalStates.Core_Temperature >= 300 and GlobalStates.Core_Temperature < 350:
            GlobalStates.AddAccident(self.accidents[4])          
        elif GlobalStates.Core_Temperature >= 350:
            GlobalStates.AddAccident(self.accidents[5])      
        
        GlobalStates.PerformAccidents()      
     
        self.round += 1
                
    def perform_action(self, action):

        action._is_done = True
        
        GlobalStates.AddCounterMeasureAction(action)
        
        if( action.action == ACTION_WATER_FROM_HELICOPTERS):
            SoundMachine.PlayRoundSound(SOUND_ROUND_HELICOPTER)
        elif( action.action == ACTION_COOL_WITH_SEAWATER):
            SoundMachine.PlayRoundSound(SOUND_ROUND_WATER_FROM_OCEAN)
        elif( action.action == ACTION_TOKIO_FIREMEN):
            SoundMachine.PlayRoundSound(SOUND_ROUND_FIREMEN)
        elif( action.action == ACTION_RESTORE_MAINS_POWER):
            SoundMachine.PlayRoundSound(SOUND_ROUND_SPECIAL_CABLE)
        elif( action.action == ACTION_REPAIR_COOLING_SYSTEM):
            SoundMachine.PlayRoundSound(SOUND_ROUND_RESTORE_ELECTRICITY)
        elif( action.action == ACTION_RADIATION_PROTECTION):
            SoundMachine.PlayRoundSound(SOUND_ROUND_PROTECTION_WEAR)
        elif( action.action == ACTION_FIX_VALVE_IN_COOLING_PIPES):
            SoundMachine.PlayRoundSound(SOUND_ROUND_VALVE)
        elif( action.action == ACTION_APPLY_SYNTHETIC_RESIN):
            SoundMachine.PlayRoundSound(SOUND_ROUND_SYNTHETIC_RESIN)
        elif( action.action == ACTION_PLUG_LEAKS_WITH_SODIUM_SILICATE):
            SoundMachine.PlayRoundSound(SOUND_ROUND_SODIUM_SILICATE)

        #GlobalStates.Radiation_Level -= 10
        
        action = action.action
        
        for act in self.actions:
            act.react_to(action)
    
    def is_game_over(self):
        return (len([action for action in self.actions if action.is_possible()]) <= 0) or (self.num_workers_left <= 0)

